<!--
 * @Author: 张久冬
 * @Date: 2021-12-30 15:46:36
 * @
# Description:
-->
<template>
  <div class="page-content">
    <JDTable
      :TabelData="ListData"
      :TabelDataCount="TabelDataCount"
      @selectionChange="selectionChange"
      v-bind="contentTableConfig"
      v-model:pageInfo="pageInfo"
      @update:pageInfo="handlePageInfoChange"
    >
      <template #enable="scoped">
        <el-button
          :type="scoped.row.enable ? 'success' : 'danger'"
          size="mini"
          >{{ scoped.row.enable ? '启用' : '禁用' }}</el-button
        >
      </template>
      <template #createAt="createAt">
        <span>{{ $filters.formatUTCTime(createAt.row.createAt) }}</span>
      </template>
      <template #updateAt="updateAt">
        <span>{{ $filters.formatUTCTime(updateAt.row.updateAt) }}</span>
      </template>
      <template #handler>
        <el-button type="text">
          <el-icon><edit /></el-icon>
          编辑
        </el-button>
        <el-button type="text">
          <el-icon><delete /></el-icon>
          删除
        </el-button>
      </template>

      <!-- 在page-content中动态插入剩余的插槽 -->
      <template
        v-for="item in otherPropSlots"
        :key="item.prop"
        #[item.slotName]="scope"
      >
        <template v-if="item.slotName">
          <slot :name="item.slotName" :row="scope.row"></slot>
        </template>
      </template>
      <!-- <template #headhandler>
        <el-button type="primary">新建用户</el-button>
      </template> -->
    </JDTable>
  </div>
</template>

<script lang="ts">
import { defineComponent, computed, reactive } from 'vue'
import JDTable from '@/base-ui/table'
import { useStore } from '@/store/index'

export default defineComponent({
  props: {
    contentTableConfig: {
      type: Object,
      required: true
    },
    pageName: {
      type: String,
      required: true
    }
  },
  components: { JDTable },
  setup(props) {
    // 页面分页信息
    const pageInfo = reactive({
      currentPage: 1,
      pageSize: 10
    })
    // 发送网络请求获取用户列表数据
    const Store = useStore()
    const getPageData = (queryInfo: any = {}) => {
      console.log('请求数据函数被调用getPageData', queryInfo)
      Store.dispatch('system/getPageListAction', {
        pageName: props.pageName,
        queryInfo: {
          offset: (pageInfo.currentPage - 1) * pageInfo.pageSize,
          size: pageInfo.pageSize,
          ...queryInfo
        }
      })
    }
    getPageData()
    // 从vuex中获取数据
    const ListData = computed(() =>
      Store.getters[`system/pageListData`](props.pageName)
    )
    console.log('菜单数据ListData', ListData.value)

    const TabelDataCount = computed(() =>
      Store.getters[`system/pageDataCount`](props.pageName)
    )
    console.log(props.pageName + '菜单数据ListData', TabelDataCount.value)

    // 4.获取其他的动态插槽名称
    const otherPropSlots = props.contentTableConfig?.TableConfig.filter(
      (item: any) => {
        if (item.slotName === 'status') return false
        if (item.slotName === 'createAt') return false
        if (item.slotName === 'updateAt') return false
        if (item.slotName === 'handler') return false
        return true
      }
    )
    // 选中table内部行数的回调函数
    const selectionChange = (selectionRow) => {
      console.log('选中', selectionRow)
    }

    const handlePageInfoChange = (e) => {
      pageInfo.currentPage = e.currentPage
      pageInfo.pageSize = e.pageSize
      getPageData()
    }
    return {
      selectionChange,
      ListData,
      TabelDataCount,
      getPageData,
      pageInfo,
      handlePageInfoChange,
      otherPropSlots
    }
  }
})
</script>

<style scoped>
.page-content {
  padding: 50px;
  border-top: 20px solid #f5f5f5;
}
</style>
